<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use Hooklife\ThinkphpWechat\Wechat;
use EasyWeChat\Foundation\Application;
class MeiController extends AdminBaseController{
    public function _initialize(){
        $this->user_id = cmf_get_current_admin_id();
        $this->query = Db::name('wx_info')->where('user_id', $this->user_id)->count();
        if ($this->user_id != 1) {
            $this->info = Db::name('wx_info')->where('user_id', $this->user_id)->find();
            if (!empty($this->info)) {
                $options = [
                    'app_id' => $this->info['app_id'],         // AppID
                    'secret' => $this->info['app_secret'],     // AppSecret
                    'token' => $this->info['token'],          // Token
                    'aes_key' => $this->info['aes_key'],                 // EncodingAESKey，安全模式下请一定要填写！！！
                ];
                $this->app = new Application($options);
                $this->app_id = $this->info['app_id'];
                $this->secret = $this->info['app_secret'];
            } else {
                echo "<div id='notice'><h1>还没有添加公众号，请先添加公众号</h1><br><a href='http://120.27.10.160:1000/Admin/wechat/configure.html'>点击跳转配置公众号</a></div>";
            }
        }
    }
    public function _empty($name){
        return $this->fetch($name);
    }

    public function businessList(){
        if($this->user_id == 1){
//            $where= '';
            $keyword = $this->request->param('keyword');
            if(!empty($keyword)){
                $where['name'] = ['like',"%".$keyword."%"];
            }
            $where['a.type'] = 'mei';
            $data = Db::table('wy_mei_business')
                ->alias('a')
                ->join('wy_wx_info b','a.info_id = b.id','left')
                ->where($where)
                ->field('a.*,b.name as infoName')
                ->select();
            return view('',[
                'data' => $data,
            ]);
        }
    }
    public function businessAdd(){
        $id = input('id');
        if($this->user_id == 1){
            $where= '';
            $con= '';
            $res= '';
            $keyword = $this->request->param('keyword');
            if(!empty($keyword)){
                $where['name'] = ['like',"%".$keyword."%"];
            }
            if(!empty($id)){
                $con['id'] = $id;
                $res = Db::table('wy_mei_business')
                    ->alias('a')
                    ->where($con)
                    ->find();
            }

            $province = Db::name('hat_province')->select();
            $city = Db::name('hat_city')->select();
            $area = Db::name('hat_area')->select();
            if(isset($res['province'])){
                foreach ($province as $k1=>$v1){
                    if($v1['provinceID'] == $res['province']){
                        $res['province'] = $v1['province'];
                        $res['provinceID'] = $v1['provinceID'];
                    }
                }
            }
            if(isset($res['city'])){
                foreach ($city as $k2=>$v2){
                    if($v2['cityID'] == $res['city']){
                        $res['city'] = $v2['city'];
                        $res['cityID'] = $v2['cityID'];
                    }
                }
            }
            if(isset($res['area'])){
                foreach ($area as $k3=>$v3){
                    if($v3['areaID'] == $res['area']){
                        $res['area'] = $v3['area'];
                        $res['areaID'] = $v3['areaID'];
                    }
                }
            }

            $data = Db::name('mei_business')->where($where)->select();
            $info = Db::name('wx_info')->select();
            $cate = Db::name('mei_cate')->select();
            return view('',[
                'data' => $data,
                'info' => $info,
                'res' => $res,
                'jiacate' => $cate,
            ]);
        }
    }
    public function businessSave(){
        $data = input();
        $id = input('id');
        $account = input('account');
        if(!empty($data)){
            $data['user_id'] = $this->user_id;
            $data['create_time'] = time();
            $cz = Db::name('mei_business')->where('account',$account)->find();
            if(!empty($cz)&&$cz['id']!=$id){
                $this->error('账号已存在');
            }else{
                if(!empty($id)){
                    $res = Db::name('mei_business')->update($data);
                }else{
                    $res = Db::name('mei_business')->insert($data);
                }
                if($res){
                    $this->success('保存成功');
                }
            }
        }
    }
    public function businessPas(){
        $id = input('id');
        $psd = input('psd');
        $data = Db::name('mei_business')->where('id',$id)->find();
        if(!empty($data)){
            if(!empty($psd)){
                $res = Db::name('mei_business')->where('id',$id)->update(['password'=>$psd]);
                if($res){
                    $this->success('修改密码成功');
                }
            }else{
                $this->success('查询密码成功,编辑可修改','',$data['password']);
            }
        }
    }
    public function businessAccount(){

    }
    public function del(){
        $table = input('table');
        $id = input('id');
        if(!empty($id)){
            $res = Db::name($table)->delete($id);
            if($res){
                $this->success('删除成功');
            }
        }
    }

    public function businesscate(){
        $cate = Db::name('mei_cate')->select();
        return view('',[
            'cate' => $cate,
        ]);
    }
    public function cateSave(){
        $name = input('name');
        $num = Db::name('mei_cate')->where(['name'=>$name])->count();
        if($num == 0){
            $res = Db::name('mei_cate')->insert(['name'=>$name]);
            if($res){
                $this->success('添加成功');
            }
        }else{
            $this->success('服务名称已经存在');
        }
    }

    public function jiaList(){
        if($this->user_id == 1){
//            $where= '';
            $keyword = $this->request->param('keyword');
            if(!empty($keyword)){
                $where['a.name'] = ['like',"%".$keyword."%"];
            }
            $where['a.type'] = 'jia';
            $data = Db::table('wy_mei_business')
                ->alias('a')
                ->join('wy_jia_cate b','a.cate_id = b.id','left')
                ->where($where)
                ->field('a.*,b.name as infoName')
                ->select();
            return view('',[
                'data' => $data,
            ]);
        }
    }
    public function jiaAdd(){
        $id = input('id');
        if($this->user_id == 1){
            $where= '';
            $con= '';
            $res= '';
            if(!empty($id)){
                $con['id'] = $id;
                $res = Db::table('wy_mei_business')
                    ->alias('a')
                    ->where($con)
                    ->find();
            }

            $province = Db::name('hat_province')->select();
            $city = Db::name('hat_city')->select();
            $area = Db::name('hat_area')->select();
            if(isset($res['province'])){
                foreach ($province as $k1=>$v1){
                    if($v1['provinceID'] == $res['province']){
                        $res['province'] = $v1['province'];
                        $res['provinceID'] = $v1['provinceID'];
                    }
                }
            }
            if(isset($res['city'])){
                foreach ($city as $k2=>$v2){
                    if($v2['cityID'] == $res['city']){
                        $res['city'] = $v2['city'];
                        $res['cityID'] = $v2['cityID'];
                    }
                }
            }
            if(isset($res['area'])){
                foreach ($area as $k3=>$v3){
                    if($v3['areaID'] == $res['area']){
                        $res['area'] = $v3['area'];
                        $res['areaID'] = $v3['areaID'];
                    }
                }
            }

            $data = Db::name('mei_business')->where($where)->select();
            $info = Db::name('wx_info')->select();
            $jiaCate = Db::name('jia_cate')->where('type','jia')->select();
            return view('',[
                'data' => $data,
                'info' => $info,
                'res' => $res,
                'jiacate' => $jiaCate,
            ]);
        }
    }
    public function jiacate(){
        $cate = Db::name('jia_cate')->where('type','jia')->select();
        return view('',[
            'cate' => $cate,
        ]);
    }
    public function jiacateSave(){
        $name = input('name');
        $type = input('type');
        $num = Db::name('jia_cate')->where(['name'=>$name])->count();
        if($num == 0){
            $res = Db::name('jia_cate')->insert(['name'=>$name,'type'=>$type]);
            if($res){
                $this->success('添加成功');
            }
        }else{
            $this->success('服务名称已经存在');
        }
    }

    public function tuoList(){
        if($this->user_id == 1){
//            $where= '';
            $keyword = $this->request->param('keyword');
            if(!empty($keyword)){
                $where['a.name'] = ['like',"%".$keyword."%"];
            }
            $where['a.type'] = 'tuo';
            $data = Db::table('wy_mei_business')
                ->alias('a')
                ->join('wy_jia_cate b','a.cate_id = b.id','left')
                ->where($where)
                ->field('a.*,b.name as infoName')
                ->select();
            return view('',[
                'data' => $data,
            ]);
        }
    }
    public function tuoAdd(){
        $id = input('id');
        if($this->user_id == 1){
            $where= '';
            $con= '';
            $res= '';
            if(!empty($id)){
                $con['id'] = $id;
                $res = Db::table('wy_mei_business')
                    ->alias('a')
                    ->where($con)
                    ->find();
            }

            $province = Db::name('hat_province')->select();
            $city = Db::name('hat_city')->select();
            $area = Db::name('hat_area')->select();
            if(isset($res['province'])){
                foreach ($province as $k1=>$v1){
                    if($v1['provinceID'] == $res['province']){
                        $res['province'] = $v1['province'];
                        $res['provinceID'] = $v1['provinceID'];
                    }
                }
            }
            if(isset($res['city'])){
                foreach ($city as $k2=>$v2){
                    if($v2['cityID'] == $res['city']){
                        $res['city'] = $v2['city'];
                        $res['cityID'] = $v2['cityID'];
                    }
                }
            }
            if(isset($res['area'])){
                foreach ($area as $k3=>$v3){
                    if($v3['areaID'] == $res['area']){
                        $res['area'] = $v3['area'];
                        $res['areaID'] = $v3['areaID'];
                    }
                }
            }


            $data = Db::name('mei_business')->where($where)->select();
            $info = Db::name('wx_info')->select();
            $jiaCate = Db::name('jia_cate')->where('type','tuo')->select();
            return view('',[
                'data' => $data,
                'info' => $info,
                'res' => $res,
                'jiacate' => $jiaCate,
            ]);
        }
    }
    public function tuocate(){
        $cate = Db::name('jia_cate')->where('type','tuo')->select();
        return view('',[
            'cate' => $cate,
        ]);
    }

    public function advert(){
        $res = Db::name('advert')->order('addtime desc')->select();
        return view('',[
            'data'=>$res,
        ]);
    }
    public function advertAdd(){
        $id = input('id');
        $res = '';
        if(!empty($id)){
            $res = Db::name('advert')->where('id',$id)->find();
            isset($res['start_time'])?$res['start_time']=date('Y-m-d',$res['start_time']):'';
            isset($res['end_time'])?$res['end_time']=date('Y-m-d',$res['end_time']):'';
        }
        return view('',[
            'res'=>$res,
        ]);
    }
    public function advertSave(){
        $data['title'] = input('title');
        $data['url'] = input('url');
        $data['yuan'] = input('yuan');
        $data['xian'] = input('xian');
        $data['start_time'] = strtotime(input('start_time'));
        $data['end_time'] = strtotime(input('end_time'));
        $data['addtime'] = time();
        $id = input('id');

        $file = request()->file('img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if($info){
                $data['img'] = $info->getSaveName();
            }else{
                echo $file->getError();
            }
        }
        if(!empty($id)){
            $data['img'] = input('img1');
            $res = Db::name('advert')->where('id',$id)->update($data);
        }else{
            $res = Db::name('advert')->insert($data);
        }

        if($res){
            $this->success('保存信息成功','advert');
        }else{
            $this->error('保存信息成功','advert');
        }
    }

    public function weiList(){
        if($this->user_id == 1){
//            $where= '';
            $keyword = $this->request->param('keyword');
            if(!empty($keyword)){
                $where['a.name'] = ['like',"%".$keyword."%"];
            }
            $where['a.type'] = 'wei';
            $data = Db::table('wy_mei_business')
                ->alias('a')
                ->join('wy_jia_cate b','a.cate_id = b.id','left')
                ->where($where)
                ->field('a.*,b.name as infoName')
                ->select();
            return view('',[
                'data' => $data,
            ]);
        }
    }
    public function weiAdd(){
        $id = input('id');
        if($this->user_id == 1){
            $where= '';
            $con= '';
            $res= '';
            if(!empty($id)){
                $con['id'] = $id;
                $res = Db::table('wy_mei_business')
                    ->alias('a')
                    ->where($con)
                    ->find();
            }
            $province = Db::name('hat_province')->select();
            $city = Db::name('hat_city')->select();
            $area = Db::name('hat_area')->select();
            if(isset($res['province'])){
                foreach ($province as $k1=>$v1){
                    if($v1['provinceID'] == $res['province']){
                        $res['province'] = $v1['province'];
                        $res['provinceID'] = $v1['provinceID'];
                    }
                }
            }
            if(isset($res['city'])){
                foreach ($city as $k2=>$v2){
                    if($v2['cityID'] == $res['city']){
                        $res['city'] = $v2['city'];
                        $res['cityID'] = $v2['cityID'];
                    }
                }
            }
            if(isset($res['area'])){
                foreach ($area as $k3=>$v3){
                    if($v3['areaID'] == $res['area']){
                        $res['area'] = $v3['area'];
                        $res['areaID'] = $v3['areaID'];
                    }
                }
            }
            $data = Db::name('mei_business')->where($where)->select();
            $info = Db::name('wx_info')->select();
            $jiaCate = Db::name('jia_cate')->where('type','wei')->select();
            return view('',[
                'data' => $data,
                'info' => $info,
                'res' => $res,
                'jiacate' => $jiaCate,
            ]);
        }
    }
    public function weicate(){
        $cate = Db::name('jia_cate')->where('type','wei')->select();
        return view('',[
            'cate' => $cate,
        ]);
    }
}